// index.js
// const app = getApp()
const { envList } = require('../../envList.js');

Page({
  data: {
    navData: [],
    contentData: [],
    showLoading: true,
    // 搜索框值
    searchVal: '',
    activeTab: 0,
    // 切割完第一个title后的数据
    titleData: [],

    routerList: ['../iconpages/rankinglist/rankinglist', '../iconpages/classify/classify', '../iconpages/newbook/newbook', '../iconpages/bookend/bookend'],

    isDark:''
  },

  onChange(event) {
    this.setData({
      activeTab: event.detail.name
    })
    this.getContentList(this.data.activeTab + 1)
  },

  onLoad() {
    this.getNavTabList()

    let storageDark = wx.getStorageSync('storageDark') ? wx.getStorageSync('storageDark') : false
    this.setData({
      isDark: storageDark
    })
   
    if (this.data.isDark) {
       wx.setNavigationBarColor({
         backgroundColor: '#000',
         frontColor: '#ffffff',
       })
       wx.setTabBarStyle({
         backgroundColor: '#000',
       })
    }

    // wx.request(

    //   {
    //     url: "https://app.kujiang.com/v1/review/get_review_list",

    //     header: {
    //       "Content-Type": "application/x-www-form-urlencoded"
    //     },
    //     method: "POST",
    //     data: {
    //       book: 65940,
    //       page: 1
    //     },
    //     dataType: "json",
    //     complete: function (res) {
    //       console.log("用户数据发送成功")
    //     },
    //     success:(res)=>{
    //       console.log(res.data);
    //     }
    //   })




  },

  getNavTabList() {
    wx.request({
      url: 'https://app.kujiang.com/v1/home/get_home_channel',
      success: (res) => {
        this.setData({
          navData: res.data.body.tab_item,
        })
        console.log(this.data.navData);
        this.getContentList(this.data.activeTab + 1)
      }
    })
  },

  getContentList(id) {
    this.setData({
      showLoading: true,
      contentData: [],
      titleData: []
    })
    wx.request({
      url: `https://app.kujiang.com/v1/home/get_book_home_data?page=1&channel_id=${id}`,
      success: (res) => {
        // console.log(res.data);
        this.setData({
          contentData: res.data.body,
          showLoading: false,
          titleData: res.data.body.slice(3)
        })
        console.log(this.data.contentData);
        console.log('titleData=>', this.data.titleData);
      }
    })
  },

  changeRoute(e) {
    let i = e.target.dataset.index
    // console.log(this.data.routerList[i]);
    wx.navigateTo({
      url: this.data.routerList[i]+'?text='+e.target.dataset.text+'&&code='+this.data.navData[this.data.activeTab].code,
      
    })
  },


  goToSearchPage(){
    wx.navigateTo({
      url: '/pages/search/search',
    })
  }



});
